<?php
/**
 * Date: 2018/3/21
 * Time: 16:13
 * Author: zeronlee
 */
namespace App\Http\Controllers\Traits;

use App\Http\Controllers\Traits\Base;
use App\Models\Access\User\User;
use Illuminate\Http\Request;

trait Users{
    /**
     * 获取有效的用户数据
     */
    public function getUsers (){
        $userFields = ['u.id','u.name as user_name','u.name','u.real_name'];
        $userWhere[0] = 1;
        if (!$this->userIsSuperAdmin()){
            $scopeWhere = $this->scopeFilter('ogz');
            $userWhere[2] = ['ao.ogz_id',$scopeWhere];
        }
        $list = User::getList($userWhere,[0,User::getCount($userWhere)],$userFields);
        foreach ($list as $item) {

            $item->name = $item->real_name . '(' .$item->user_name . ')';
        }
        return $list;
    }
    /**
     * 获取用户名
     * @param string $ids
     * @return string
     */
    public function getUserName ($ids){
        $user_name = '';
        //第一版本联系人一对一 之后改为一对多,不影响原来的代码大局,不改动model的关联join
        $users = User::select(['real_name','name'])->whereIn('id',explode(',',$ids))->get();
        //禁用用户不能参与操作
        foreach ($users as $user) {
            $user_name .= $user->real_name . '(' . $user->name . '),';
        }
        $user_name = trim($user_name,',');
        return $user_name;
    }
}